﻿using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Swashbuckle.AspNetCore.Swagger;
using OnlineCourse.API.Helper;
using OnlineCourse.Model;
using OnlineCourse.JWTAuthorizePolicy;
using OnlineCourse.Services;

namespace OnlineCourse.API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //读取用户配置文件（订阅人信息发行人信息）
            var audienceConfig = Configuration.GetSection("Audience");
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info
                {
                    Version = "v1",
                    Title = "OnlineCourse.API接口文档",
                    Description = "RESTful API for OnlineCourse.API",
                    TermsOfService = "None",
                    Contact = new Contact { Name = "zj", Email = "24002010@qq.com", Url = "" }
                });

                c.OperationFilter<HttpHeaderOperation>(); // 添加httpHeader参数
            });

            services.AddPolicyJwtBearer(audienceConfig["Issuer"], audienceConfig["Issuer"], audienceConfig["Secret"], "MXJBearer", "Permission", "api/error/denied");
            
            //读取数据库配置信息
            var connectionStrings = Configuration.GetSection("ConnectionStrings");
            services.AddDbContextPool<ApplicationDbContext>(options => options.UseSqlServer(connectionStrings["DefaultConnection"], a => a.MigrationsAssembly("OnlineCourse.API")));
           
            services.AddMvc(options => options.Filters.Add(typeof(CustomExceptionFilterAttribute)))
                .AddJsonOptions(options =>
                {
                    options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";
                    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseAuthentication();

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "WebApi V1");
            });


            app.UseMvc();
        }
    }
}
